package com.demo.config;


import com.demo.interceptor.AuthorizationInterceptor;
import common.constant.Constants;
import common.utils.annotations.DesensitizationAnnotationFormatterFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * MVC配置
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private AuthorizationInterceptor authorizationInterceptor;



    @Override
    public void addFormatters(FormatterRegistry registry) {
        //添加自定义注解
        registry.addFormatterForFieldAnnotation(new DesensitizationAnnotationFormatterFactory());
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //默认访问接口文档地址
        registry.addViewController("/").setViewName("forward:/doc.html");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 所有接口都需token验证,排除不需要的
        registry.addInterceptor(authorizationInterceptor).addPathPatterns("/api/**","/common/**").excludePathPatterns(outUrls());
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        
    }

    /**
     * 设置跨域访问
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //设置允许跨域的路径
        registry.addMapping("/**")
                //设置允许跨域请求的域名
                .allowedOriginPatterns("*")
                //是否允许证书 不再默认开启
                .allowCredentials(true)
                //设置允许的方法
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                //跨域允许时间
                .maxAge(7200);
    }

    /**
     * 所有排除不需要token的urls
     *
     * @return
     */
    public List<String> outUrls() {
        List<String> arrayList = new ArrayList<>(16);

        arrayList.add(Constants.RESOURCE_UPLOAD + "/**");
        arrayList.add(Constants.RESOURCE_DOWNLOAD + "/**");
        arrayList.add("/v2/api-docs");
        arrayList.add("/statics/**");
        arrayList.add("/static/**");
        arrayList.add("/swagger-ui.html");
        arrayList.add("/doc.html");
        arrayList.add("/swagger-resources/**");
        arrayList.add("/webjars/**");
        arrayList.add("/*/*.html");
        arrayList.add("/*/*.js");
        arrayList.add("/*/*.css");

        //不需要认证接口
        arrayList.add("/**/noauth/**");

        //首页公司信息
        arrayList.add("/api/home/companyInfo");
        //案例列表
        arrayList.add("/api/home/caseList");
        //案例分类列表
        arrayList.add("/api/home/caseClassificationList");
        //案例颜色列表
        arrayList.add("/api/home/caseColorList");
        //案例风格列表
        arrayList.add("/api/home/caseStyleList");
        //庆典类型列表
        arrayList.add("/api/home/celebrationTypeList");

        //加盟信息
        arrayList.add("/api/my/franchiseInfo");
        //行业入驻列表
        arrayList.add("/api/my/franchiseTypeList");

        //微信登录
        arrayList.add("/api/wxLogin");
        //微信支付回调
        arrayList.add("/wxpay/callback");
        //微信退款回调
        arrayList.add("/wxrefund/callback");
        return arrayList;
    }
}